package com.taxshare.seller.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.DocumentInfoDetailEntity;
import com.taxshare.seller.mapper.DocumentInfoDetailMapper;
import com.taxshare.seller.view.dto.DocumentInvoiceDetailDTO;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hjh
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
@Slf4j
public class DocumentInfoDetailService extends ServiceImpl<DocumentInfoDetailMapper, DocumentInfoDetailEntity> {

    static final Double TAX_RATE=0.00;
    /**
     * 查询发票明细（明源）
     * @param query
     * @return
     */
    public Response queryAllDetail(PageRequest<DocumentInvoiceDetailDTO> query) {
        DocumentInvoiceDetailDTO info = query.getInfo();
        QueryWrapper<DocumentInfoDetailEntity> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(info.getPaymentName())){
            queryWrapper.like("payment_name",info.getPaymentName());
        }
        queryWrapper.eq("doc_id",info.getDocId());
        queryWrapper.orderByDesc("create_time");
        IPage<DocumentInfoDetailEntity> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), DocumentInfoDetailEntity.class);
        return Response.success(result);
    }

    /**
     * 新增发票明细（明源）
     * @param info
     * @return
     */
    public Response addDetail(DocumentInvoiceDetailDTO info) {
        QueryWrapper<DocumentInfoDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("doc_id", info.getDocId());
        List<DocumentInfoDetailEntity> list = this.list(queryWrapper);
        DocumentInfoDetailEntity documentInfoDetailEntity = new DocumentInfoDetailEntity();
        BeanUtil.copyProperties(info, documentInfoDetailEntity);
        if (list != null && !list.isEmpty()) {
            documentInfoDetailEntity.setTaxRateFlag(list.get(0).getTaxRateFlag());
        }
        documentInfoDetailEntity.setUuid(UUIDUitl.UUID32(false));
        documentInfoDetailEntity.setDataSource("1");
        this.save(documentInfoDetailEntity);
        return Response.success();
    }

    /**
     * 更新发票明细
     * @param info
     * @return
     */
    public Response updateDetail(DocumentInvoiceDetailDTO info) {
        QueryWrapper<DocumentInfoDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",info.getUuid());
        DocumentInfoDetailEntity one = this.getOne(queryWrapper);
        BeanUtil.copyProperties(info,one);
        this.updateById(one);
        return Response.success();
    }

    /**
     * 删除发票明细
     * @param info
     * @return
     */
    public Response deleteDetail(DocumentInvoiceDetailDTO info) {
        QueryWrapper<DocumentInfoDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid",info.getUuid());
        this.remove(queryWrapper);
        return Response.success();
    }
}
